<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class Proxy extends Model{
//    protected $table='think_article_proxy';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_member.open_id,
        think_member.nickname";
        $list = $this
            ->field($field)
            ->join('think_member',"think_member.id = {$this->getTable()}.mid")
            ->order($this->getTable().'.create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    public function getUpdateTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

    /**
     * @param int $id
     * @param int $status
     * @param string $desc
     * @param string $out
     * @return bool|float
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function updateStatus($id=0,$status=0,$desc='',&$out=''){
        $info = $this->find($id);
        if(!$info){
            $out = '申请不存在';
            return false;
        }
        $this->startTrans();
        if($status==3){
            if(!$desc){
                $out = '请输入拒绝理由';
                return false;
            }
            $info->info = $desc;
            $info->status = $status;
            $info->update_time = time();
            if(!$info->save()){
                $this->rollback();
                $out = '操作失败';
                return false;
            }
            $this->commit();
            return true;
        }elseif($status==2){
            $info->status = $status;
            $info->update_time = time();
            $result = $info->save();
            if(!$result){
                $this->rollback();
                $out = '操作失败';
                return false;
            }
            $this->commit();
            return true;
        }else{
            $out = '错误的请求';
            $this->rollback();
            return false;
        }
    }

    /**
     * 活动红包
     * @param $name
     * @param $aid
     * @param $mc_id
     * @param $fee
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getArticleMoney($name,$aid,$mc_id,$fee,&$out=''){
        $money = new ProxyMoney();
        $fee = ($fee * (1-0.006)) * 0.3;    // 实际 * (1-0.006) * 0.3
        $result = $money->addNew($name,$aid,$mc_id,$fee,1,0,$out1);
        if(!$result){
            $out = $out1;
            return false;
        }
        if(!$result->save()){
            $out = '操作失败';
            return false;
        }
        return true;
    }

    /**
     *
     * @param $name
     * @param $aid
     * @param $mc_id
     * @param $fee
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getVipMoney($name,$aid,$mc_id,$fee,&$out=''){
        $money = new ProxyMoney();
        $fee = ($fee * (1-0.006)) * 0.3;
        $result = $money->addNew($name,$aid,$mc_id,$fee,3,0,$out1);
        if(!$result){
            $out = $out1;
            return false;
        }
        if(!$result->save()){
            $out = '操作失败';
            return false;
        }
        return true;
    }

    /**
     * 获取代理openid
     * @param $id
     * @return mixed
     */
    public function getOpenIdByProxyId($id){
       $info = $this->find($id);
       $member = (new Member())->get($info['mid']);
       return $member['open_id'];
    }


    protected function getInfoAttr($value){
        return $value?$value:'--';
    }

}